//
//  ViewController.m
//  MyWeChat
//
//  Created by 蔡欣东 on 15/10/5.
//  Copyright © 2015年 蔡欣东. All rights reserved.
//

#import "LoginViewController.h"

#import "AppDelegate.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginViewController:UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}
- (IBAction)LoginButtonClick:(UIButton *)sender {
    if ([self.usernameTextField.text isEqual:@""]||[self.passwordTextField.text isEqual:@""]) {
        return;
    }
    
    NSString* trimUsername = [self.usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSString* name = [NSString stringWithFormat:@"%@@458770054.local",trimUsername];
    [[NSUserDefaults standardUserDefaults] setValue:name forKey:USERNAME];
    
    NSString* trimPassword = [self.passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    [[NSUserDefaults standardUserDefaults] setValue:trimPassword forKey:PASSWORD];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [(AppDelegate *)[UIApplication sharedApplication].delegate login];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"releaseSource" object:nil];
    
    ((AppDelegate *)[UIApplication sharedApplication].delegate).tabbarController.selectedIndex = 0;
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == self.usernameTextField){
        [self.usernameTextField resignFirstResponder];
        [self.passwordTextField becomeFirstResponder];
    }else{
        [self.passwordTextField resignFirstResponder];
        [self LoginButtonClick:self.loginButton];
    }
    return false;
}
@end
